/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
/**
* $Log: LoggingSocket.java,v $
* Revision 1.2  2004/09/10 11:27:05  jaspervdb
* no message
*
* Revision 1.1  2004/09/06 11:27:06  jaspervdb
* no message
*
* Revision 1.2  2003/09/30 12:36:25  jasperv
* no message
*
* Revision 1.5  2002/11/28 06:09:48  jasperv
* Completed Command System. VRMS activations complete. UA testing begined
*
* Revision 1.3  2002/05/15 13:51:28  JasperV
* Updated Logging System (static logging)
*
* Revision 1.2  2002/05/14 08:07:56  travisb
* New Static Logging System
*
* Revision 1.1  2002/02/20 13:30:39  JasperV
* First time add to new Applications layer
*
* Revision 1.1  2002/01/24 14:18:14  travisb
* First Version of the Application Layer
*
*/

package blueprint4j.utils;

import java.io.*;
import java.net.*;
import java.text.*;
import java.util.*;

public class LoggingSocket extends Logging implements Runnable {
	private SimpleDateFormat date_format=new SimpleDateFormat("yyyy-MM-dd 'at' HH'h'mm.ss");
	private ServerSocket server_socket = null;
	public Vector sockets = new Vector();

	public LoggingSocket(int port)
	throws IOException {
		super(true, true, true, true,true,true);
		server_socket = new ServerSocket(port);
		new Thread(this).start();
	}

	public void write(String level,long thread_id,String description, String details) {
		String msg =
		   "\r\n\r\n"+level+" ["+thread_id+"] " + date_format.format(new Date()) + "\r\n" +
		   "TCE " + date_format.format(new Date()) + "\r\n" +
		   "Description: " + description + "\r\n" +
		   details + "\r\n" +
		   "-------------------------------------";
		for (int i = 0;i < sockets.size();i++) {
			((SocketLog)sockets.get(i)).logs.add(msg);
		}
	}

	public void run() {
		while (ThreadSchedule.isStillAlive()) {
			try {
				SocketLog log = new SocketLog(this,server_socket.accept());
				sockets.add(log);
				new Thread(log).start();
			} catch (Throwable th) {}
		}
	}

	public LogDataUnitVector findLogs(Integer tid,java.util.Date from_date,java.util.Date to_date,Logging.Level levels[])
	throws IOException {
		return null;
	}

	public static class SocketLog implements Runnable {
		private LoggingSocket logging_socket=null;
		private Socket socket = null;
		public Vector logs = new Vector();

		public SocketLog(LoggingSocket logging_socket, Socket socket) {
			this.logging_socket = logging_socket;
			this.socket = socket;
		}

		public void run() {
			try {
				while (true) {
					if (logs.size() > 0) {
						socket.getOutputStream().write(logs.get(0).toString().getBytes());
						logs.remove(0);
					}
					Thread.sleep(250);
				}
			} catch (Throwable th) {
				logging_socket.sockets.remove(this);
			}
		}
	}
}

